package cn.wolfcode.education.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public abstract class MailUtil {
    public static String sendEamilCode(String eamil) {
        HtmlEmail send = new HtmlEmail();
        //获取随机验证码
        String[] beforeShuffle = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
                "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
                "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z"};
        List list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String resultCode = afterShuffle.substring(3, 9);
        //拿到验证码  开始走发送

        try {
            send.setHostName("smtp.qq.com");
            send.setSmtpPort(465);     //端口号
            send.setSSLOnConnect(true);   //开启SSL加密
            send.setCharset("utf-8");
            send.addTo(eamil);   //接收者的QQEamil
            //第一个参数是发送者的QQEamil   第二个参数是发送者QQ昵称
            send.setFrom("mwb.sr@qq.com", "天河集团");
            //第一个参数是发送者的QQEamil   第二个参数是刚刚获取的授权码
            send.setAuthentication("mwb.sr@qq.com", "ivzhllbpzwxdbajd");
            //Eamil的标题  第一个参数是我写的判断上下午，删掉即可
            send.setSubject("天河集团管理员");
            send.setMsg("天河集团 本次验证码:" + resultCode + "   30分钟内有效");   //Eamil的内容
            send.send(); //发送
        } catch (EmailException e) {
            e.printStackTrace();
        }
        return resultCode; //返回校验码
    }
    @Test
    public void Test(){
        String code = MailUtil.sendEamilCode("1028846125@qq.com");
        System.out.print(code);
    }
}
